package shop

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type CategoryOptionsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 类型下拉列表
func NewCategoryOptionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CategoryOptionsLogic {
	return &CategoryOptionsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CategoryOptionsLogic) CategoryOptions(req *types.PmsCategoryOptionsReq) (resp []*types.PmsCategoryOption, err error) {
	var items []*model.PmsCategoryModel
	l.svcCtx.GormClient.GormDb.Model(model.PmsCategoryModel{}).
		Where("shop_id = ?", req.Id).
		Order("sort desc ,id asc ").
		Find(&items)

	return makeTreeCategoryOption(items, 0), nil
}

func makeTreeCategoryOption(list []*model.PmsCategoryModel, parentID int64) []*types.PmsCategoryOption {
	res := make([]*types.PmsCategoryOption, 0)
	for _, item := range list {
		if item.ParentId == parentID {
			var ot types.PmsCategoryOption
			ot.Label = item.Name
			ot.Value = item.Id
			ot.Children = makeTreeCategoryOption(list, item.Id)
			res = append(res, &ot)
		}
	}
	return res
}
